package com.inplaceedit.client.ui.support;

import com.google.web.bindery.autobean.shared.AutoBean;
import com.google.web.bindery.autobean.shared.AutoBeanCodex;
import com.google.web.bindery.autobean.shared.AutoBeanFactory;
import com.google.web.bindery.autobean.shared.AutoBeanUtils;

public class AutoBeanTools {
	/**
	 * Clones an auto-bean object by encoding to and then decoding from JSON.
	 * 
	 * @param <T>
	 *            the object type
	 * @param object
	 *            the object
	 * @param clazz
	 *            the Class of type T of the object
	 * @return a copy of the object
	 */
	public static <T> T clone(final T object, final Class<T> clazz) {
		final AutoBean<T> bean = AutoBeanUtils.getAutoBean(object);
		final AutoBeanFactory factory = bean.getFactory();
		return AutoBeanCodex.decode(
				factory,
				clazz,
				AutoBeanCodex.encode(AutoBeanUtils.getAutoBean(object))
						.getPayload()).as();
	}
}
